#############################

# packages

pacman::p_load(sjmisc, plotrix, matrixStats, expss, dplyr)


#############################
##### exclude people who were not present in Wave 1, but were in Wave 3

fgz <- fgz %>%
  drop_na(wave_w1t1)

table(fgz$poccstat_w1t1)

table(fgz$poccstat_w3)


# drop if age <18

##### age
fgz <- fgz %>% 
  mutate(intyr = trunc(interviewdatum_w1t1/10000)) %>% 
  mutate(intmth = trunc(interviewdatum_w1t1/100-intyr*100)) %>% 
  mutate(agea = ifelse(pbirthmnth_w1t1>intmth, intyr - pbirthyear_w1t1-1,intyr - pbirthyear_w1t1))

fgz <- fgz %>%
  filter(agea>17)




# variable generation

# popuilsm indices

fgz <- fgz %>% 
  mutate(populism_w1 = (ppopul01_w1t1 + ppopul02_w1t1 + ppopul03_w1t1 + ppopul04_w1t1 + ppopul05_w1t1 + ppopul06_w1t1 +
                          ppopul07_w1t1 + ppopul08_w1t1 + ppopul09_w1t1) /9,
         anti.elitism_w1 = (ppopul01_w1t1 + ppopul02_w1t1 + ppopul03_w1t1) /3,
         popular.sovereignty_w1 = (ppopul04_w1t1 + ppopul05_w1t1 + ppopul06_w1t1) /3,
         homogeneity_w1 = (ppopul07_w1t1 + ppopul08_w1t1 + ppopul09_w1t1) /3,
         populism_w3 = (ppopul01_w3 + ppopul02_w3 + ppopul03_w3 + ppopul04_w3 + ppopul05_w3 + ppopul06_w3 +
                          ppopul07_w3 + ppopul08_w3 + ppopul09_w3) /9,
         anti.elitism_w3 = (ppopul01_w3 + ppopul02_w3 + ppopul03_w3) /3,
         popular.sovereignty_w3 = (ppopul04_w3 + ppopul05_w3 + ppopul06_w3) /3,
         homogeneity_w3 = (ppopul07_w3 + ppopul08_w3 + ppopul09_w3) /3 )

table(fgz$homogeneity_w3)
table(fgz$populism_w1)

# employment groups

# Public Sector Employment

table(fgz$pempcond01_w1t1)

fgz<- fgz %>% 
  mutate(pub_empl_w1 = case_when(pempcond01_w1t1 == 1  ~  1,
                                 pempcond01_w1t1 == 2  ~  0,
                                 pempcond01_w1t1 <1 ~ NA))


table(fgz$pub_empl_w1)        


table(fgz$pub_empl_w1, fgz$poccstat_w1t1,useNA = "ifany")   


table(fgz$pempcond01_w3)

fgz <- fgz %>% 
  mutate(pub_empl_w3 = case_when(pempcond01_w3== 1  ~  1,
                                 pempcond01_w3 == 2  ~  0,
                                 pempcond01_w3 <1 ~ NA))

table(fgz$pub_empl_w3)         



# Civil Servant

table(fgz$poccstat_w1t1)


fgz <- fgz %>% 
  mutate(civ_serv_w1 = case_when(poccstat_w1t1 == 1  ~  0,
                                 poccstat_w1t1 == 2  ~  1,
                                 poccstat_w1t1 == 3  ~  0,
                                 poccstat_w1t1 == 4  ~  0,
                                 poccstat_w1t1 == 5  ~  0,
                                 poccstat_w1t1 >5 ~ NA,
                                 poccstat_w1t1 <1 ~ NA))
table(fgz$civ_serv_w1)        


table(fgz$poccstat_w3)

fgz <- fgz %>% 
  mutate(civ_serv_w3 = case_when(poccstat_w3 == 1  ~  0,
                                 poccstat_w3 == 2  ~  1,
                                 poccstat_w3 == 3  ~  0,
                                 poccstat_w3 == 4  ~  0,
                                 poccstat_w3 == 5  ~  0,
                                 poccstat_w3 >5 ~ NA,
                                 poccstat_w3 <1 ~ NA))
table(fgz$civ_serv_w3)        


#Check inconsistent responses (17 inc.)


table(fgz$civ_serv_w1, fgz$pub_empl_w1) # 21 cases inconsistent


# Exclude the specified cases

table(fgz$civ_serv_w3, fgz$pub_empl_w3) #10 cases inconsistent

#change over time

table(fgz$civ_serv_w1, fgz$pub_empl_w3)

table(fgz$civ_serv_w3, fgz$pub_empl_w1)


# Create employment sector

table(fgz$pub_empl_w1)
table(fgz$civ_serv_w1)



fgz <- fgz %>%
  mutate(pub_type_w1 = case_when(
    pub_empl_w1 == 1 & civ_serv_w1 == 1 ~ 2, # Civil servants
    pub_empl_w1 == 1 & is.na(civ_serv_w1) ~ NA_integer_, # Unclear cases as NA
    pub_empl_w1 == 1 ~ 1,                    # Public employees
    pub_empl_w1 == 0 ~ 0,                    # Private employees
    TRUE ~ NA_integer_                       # Other cases as NA
  ))


table(fgz$pub_type_w1)
table(fgz$pub_type_w1, useNA = "ifany")



fgz <- fgz %>%
  mutate(pub_type_w1 = factor(pub_type_w1,
                              levels = c(0, 1, 2),
                              labels = c("Private employees", "Public employees", "Civil servants")))

table(fgz$pub_type_w1)


fgz <- fgz %>%
  mutate(pub_type_w3 = case_when(
    pub_empl_w3 == 1 & civ_serv_w3 == 1 ~ 2, # Civil servants
    pub_empl_w3 == 1 & is.na(civ_serv_w3) ~ NA_integer_, # Unclear cases as NA
    pub_empl_w3 == 1 ~ 1,                    # Public employees
    pub_empl_w3 == 0 ~ 0,                    # Private employees
    TRUE ~ NA_integer_                       # Other cases as NA
  ))


table(fgz$pub_type_w3)
table(fgz$pub_type_w3, useNA = "ifany")


fgz <- fgz %>%
  mutate(pub_type_w3 = factor(pub_type_w3,
                              levels = c(0, 1, 2),
                              labels = c("Private employees", "Public employees", "Civil servants")))

table(fgz$pub_type_w3)



# Exclude inconsistent cases and create the subsample fgz_ed
fgz <- fgz %>%
  filter(!(civ_serv_w1 == 1 & pub_empl_w1 == 0))

any(fgz$civ_serv_w1 == 1 & fgz$pub_empl_w1 == 0)  # Should return FALSE

# Exclude inconsistent cases and create the subsample fgz_ed
fgz <- fgz %>%
  filter(!(civ_serv_w3 == 1 & pub_empl_w3 == 0))

any(fgz$civ_serv_w3 == 1 & fgz$pub_empl_w3 == 0)  # Should return FALSE


#check

table(fgz$pub_type_w1, useNA = "ifany")

table(fgz$pub_type_w1, useNA = "ifany")



table(fgz$pub_type_w3, useNA = "ifany")

table(fgz$pub_type_w3, useNA = "ifany")

